package com.demo.aop.pcut.dyanmic;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;

/**
 * 描述：动态切入点
 *
 * @author lida
 * @time 2019/10/23 14:37
 */
public class DynamicSimplePointcut extends DynamicMethodMatcherPointcut {
	@Override
	public boolean matches(Method method, Class<?> targetClass){
		System.out.println("Static check for " + method.getName());
		return "foo".equals(method.getName());
	}

	/**
	 * 描述：动态切入点必须重写该方法，对参数进行验证
	 * @param method the candidate method
	 * @param targetClass the target class (may be {@code null}, in which case
	 * the candidate class must be taken to be the method's declaring class)
	 * @param args arguments to the method
	 * @return boolean x > 100
	 */
	@Override
	public boolean matches(Method method, Class<?> targetClass, Object... args) {
		System.out.println("Dynamic check for " + method.getName());
		int x = (int)args[0];
		return x > 100;
	}

	@Override
	public ClassFilter getClassFilter(){
		return clazz -> (clazz == SimpleBean.class);
	}
}
